/**
 * Created by XianTao on 2015/8/5
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 请求api得到返回结果后的处理类
 */
public class ApiResult {
    private String json;
    private Map attrs;
    private List<Map> mList;

    private ApiResult(String json) {
        this.json = json;
        try {
            this.attrs = new ObjectMapper().readValue(json, Map.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private ApiResult(String json, boolean isList) {
        this.json = json;
        try {
            ObjectMapper mapper = new ObjectMapper();
            this.mList = mapper.readValue(json, mapper.getTypeFactory().constructParametricType(ArrayList.class, Map.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建对象，等同于构造函数
     *
     * @param json api返回结果
     */
    public static ApiResult create(String json) {
        return new ApiResult(json);
    }

    public static ApiResult createList(String json) {
        return new ApiResult(json, true);
    }

    public String getJson() {
        return json;
    }

    public List<Map> getMList() {
        return mList;
    }

    public String getStr(String name) {
        return (String) attrs.get(name);
    }

    public String getStr(String name, String defaultValue) {
        return attrs.get(name) == null ? defaultValue : (String) attrs.get(name);
    }

    public Integer getInt(String name) {
        return (Integer) attrs.get(name);
    }

    public BigDecimal getBigDecimal(String name) {
        return attrs.get(name) == null ? null : new BigDecimal(attrs.get(name) + "");
    }

    public List getList(String name) {
        return (List) attrs.get(name);
    }

    public Map getMap(String name) {
        return (Map) attrs.get(name);
    }

    public String getValueFromInnerAttribute(String outerName, String key, String innerName, String defaultValue) {
        Map map = (Map) attrs.get(outerName);
        List list = (List) map.get(key);
        for (Object aList : list) {
            Map inner = (Map) aList;
            if (innerName.equals(inner.get("name"))) {
                return (String) inner.get("value");
            }
        }
        return defaultValue;
    }

    public String getValueFromInnerAttribute(Map map, String key, String innerName, String defaultValue) {
        List list = (List) map.get(key);
        for (Object aList : list) {
            Map inner = (Map) aList;
            if (innerName.equals(inner.get("name"))) {
                return (String) inner.get("value");
            }
        }
        return defaultValue;
    }

    /**
     * Api请求是否成功
     */
    public boolean isSucceed() {
        Integer errCode = getErrorCode();
        return (errCode == null || errCode == 0);
    }

    public Integer getErrorCode() {
        return (Integer) attrs.get("errcode");
    }

    /**
     * Api请求是否成功
     *
     * @param key
     */
    public boolean isSucceed(String key) {
        return this.getInt(key) == 0;
    }
}